package com.qf.controller;

import com.qf.service.*;
import com.qf.util.ResponseUtil;
import com.qf.vo.CategorySellAmts;
import com.qf.vo.CategorySellVo;
import com.qf.vo.DayStatis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/dashboard")
public class AdminController {
    @Autowired
    DtsUserService dtsUserService;
    @Autowired
    DtsGoodsService dtsGoodsService;
    @Autowired
    DtsGoodsProductService dtsGoodsProductService;
    @Autowired
    DtsOrderService dtsOrderService;
    @Autowired
    DtsStatService statService;

    @RequestMapping("/chart")
    public Object chart(){
        //得到销售类别的集合
        List<CategorySellAmts> categorySellData = statService.findCategorySell();
        //数组
        //销售名称
        String[] categoryNames = new String[categorySellData.size()];
        //遍历集合得到每一种销售类别
        for (int i = 0;i < categorySellData.size();i++){
            categoryNames[i] = categorySellData.get(i).getName();
        }

        //封装数据
        CategorySellVo categorySellVo = new CategorySellVo();
        categorySellVo.setCategoryNames(categoryNames);
        categorySellVo.setCategorySellData(categorySellData);

        //获取订单天数统计集合
        List<DayStatis> orderDayStatis = statService.findOrderDayStatis();
        //提取日期
        List<String> dayData = fetchDayStr(orderDayStatis);
        //订单金额
        List<BigDecimal> orderAmtData = fetchAmtData(orderDayStatis,dayData);
        //订单数量
        List<Integer> orderCntData = fetchCntData(orderDayStatis,dayData);
        //封装订单数据
        Map<String,Object> orderAmts = new HashMap<>();
        orderAmts.put("dayData",dayData);
        orderAmts.put("orderAmtData",orderAmtData);
        orderAmts.put("orderCntData",orderCntData);

        //查询用户的数量
        List<DayStatis> userDayStatis = statService.findUserDayStatis();

        //提取日期
        List<String> dayData1 = fetchDayStr(userDayStatis);
        //两个日期合并
        dayData1 = unionDayDate(dayData,dayData1);
        //提取user的数量
        List<Integer> userCnt = fetchCntData(userDayStatis, dayData1);
        //提取订单的数量
        List<Integer> orderCnt = fetchCntData(orderDayStatis, dayData1);

        //封装用户和订单的数量
        Map<String,Object> userOrderCnt = new HashMap<>();
        userOrderCnt.put("dayData",dayData1);
        userOrderCnt.put("userCnt", userCnt);
        userOrderCnt.put("orderCnt",orderCnt);

        Map<String,Object> data = new HashMap<>();
        data.put("categorySell",categorySellVo);
        data.put("orderAmts",orderAmts);
        data.put("userOrderCnt",userOrderCnt);

        return ResponseUtil.ok(data);
    }

    /**
     * 合并两个日期
     * @param dayData
     * @param dayData1
     * @return
     */
    private List<String> unionDayDate(List<String> dayData, List<String> dayData1) {
        Set<String> set = new HashSet<>();
        //将两个日期分别封装进同一个集合
        set.addAll(dayData);
        set.addAll(dayData1);

        List<String> list = new ArrayList<>();
        //将封装两个日期的集合封装成新的集合
        list.addAll(set);

        //排序
        Collections.sort(list);

        return list;
    }

    /**
     * 获取订单数量
     * @param orderDayStatis
     * @param dayData
     * @return
     */
    private List<Integer> fetchCntData(List<DayStatis> orderDayStatis, List<String> dayData) {
        List<Integer> list = new ArrayList<>();
        for (String dayDatum : dayData) {
            for (DayStatis orderDayStati : orderDayStatis) {
                if (dayDatum.equalsIgnoreCase(orderDayStati.getDayStr())){
                    list.add(orderDayStati.getCnts());
                }
            }
        }
        return list;
    }

    /**
     * 获取订单金额
     * @param orderDayStatis--订单天数统计
     * @param dayData--每日数据
     * @return
     */
    private List<BigDecimal> fetchAmtData(List<DayStatis> orderDayStatis, List<String> dayData) {
        List<BigDecimal> list = new ArrayList<>();
        //每日数据
        for (String dayDatum : dayData) {
            //订单天数统计
            for (DayStatis orderDayStati : orderDayStatis) {

                if (dayDatum.equalsIgnoreCase(orderDayStati.getDayStr())){

                    list.add(orderDayStati.getAmts());
                }
            }
        }
        return list;
    }

    /**
     * 提取日期操作
     * @param orderDayStatis
     * @return
     */
    private List<String> fetchDayStr(List<DayStatis> orderDayStatis) {
        //获取新的日期字符串
        List<String> list = orderDayStatis.stream().map(orderDayStatis1 ->{
            return orderDayStatis1.getDayStr();
        }).collect(Collectors.toList());

        //排序
        //???
        Collections.sort(list);

        return list;
    }

    /**
     * 信息记录
     * 判断
     * @return
     */
    @RequestMapping("")
    public Object info(){
        long userTotal = dtsUserService.getTotal();
        long goodsTotal = dtsGoodsService.getTotal();
        long productTotal = dtsGoodsProductService.getTotal();
        long orderTotal = dtsOrderService.getTotal();

        Map<String,Object> data = new HashMap<>();
        data.put("userTotal",userTotal);
        data.put("goodsTotal",goodsTotal);
        data.put("productTotal",productTotal);
        data.put("orderTotal",orderTotal);
        return ResponseUtil.ok(data);
    }
}
